<?php
/**
 * 辅助函数
 */

// 如果直接访问此文件，则中止执行
if (!defined('ABSPATH')) {
    exit;
}

/**
 * 显示栖岛登录按钮
 *
 * @param bool $text 是否显示文本链接
 * @return string 登录按钮HTML
 */
function qidao_connect_button($text = false) {
    global $qidao_connect;
    
    if (!$qidao_connect) {
        $qidao_connect = new Qidao_Connect();
    }
    
    return $qidao_connect->login_button($text);
}

/**
 * 检查用户是否已绑定栖岛账号
 *
 * @param int $user_id 用户ID
 * @return bool 是否已绑定
 */
function qidao_connect_is_bound($user_id = 0) {
    if (!$user_id) {
        $user_id = get_current_user_id();
    }
    
    if (!$user_id) {
        return false;
    }
    
    global $wpdb;
    $table_name = $wpdb->prefix . 'qidao_connect';
    $result = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $table_name WHERE user_id = %d", $user_id));
    
    return $result > 0;
}

/**
 * 获取用户的栖岛账号信息
 *
 * @param int $user_id 用户ID
 * @return object|false 栖岛账号信息或false
 */
function qidao_connect_get_user_data($user_id = 0) {
    if (!$user_id) {
        $user_id = get_current_user_id();
    }
    
    if (!$user_id) {
        return false;
    }
    
    global $wpdb;
    $table_name = $wpdb->prefix . 'qidao_connect';
    $result = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table_name WHERE user_id = %d", $user_id));
    
    return $result;
}

/**
 * 解绑栖岛账号
 *
 * @param int $user_id 用户ID
 * @return bool 是否成功解绑
 */
function qidao_connect_unbind($user_id = 0) {
    if (!$user_id) {
        $user_id = get_current_user_id();
    }
    
    if (!$user_id) {
        return false;
    }
    
    global $wpdb;
    $table_name = $wpdb->prefix . 'qidao_connect';
    $result = $wpdb->delete($table_name, array('user_id' => $user_id), array('%d'));
    
    return $result !== false;
} 